#include <string>
#include "glString.h"
#include "WindowStatus.h"

// Here are the fonts: 
/*
   */

// Here is the function 

void* GLString::mFonts[] = {
	GLUT_BITMAP_9_BY_15, 
	GLUT_BITMAP_8_BY_13, 
	GLUT_BITMAP_TIMES_ROMAN_10, 
	GLUT_BITMAP_TIMES_ROMAN_24, 
	GLUT_BITMAP_HELVETICA_10, 
	GLUT_BITMAP_HELVETICA_12, 
	GLUT_BITMAP_HELVETICA_18 
};

int GLString::mCurFont = 0;

float GLString::red = 1.0f;
float GLString::green = 0.0f;
float GLString::blue = 0.0f;
float GLString::alpha = 1.0f;

void GLString::SetColor(float r, float g, float b, float a) {
	red = r;
	green = g;
	blue = b;
	alpha = a;
}

void GLString::Print(float x, float y, float z, const std::string& text)
{
	int blending = 0; 

	if(glIsEnabled(GL_BLEND)) blending = 1; 
	glEnable(GL_BLEND); 
	glColor4f(red, green, blue, alpha);
	glRasterPos3f(x,y, z); 
	for (size_t i = 0; i < text.size(); ++i) {
		glutBitmapCharacter(mFonts[mCurFont], text[i]); 
	} 
	if(!blending) glDisable(GL_BLEND); 
}

void GLString::Print2D(float x, float y, const std::string& text) {
	//glBindTexture(GL_TEXTURE_2D, font[p_font].texture_id);  //设置纹理 
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // 背景透明模式
	glEnable(GL_BLEND); // 使用混合

	// 禁止关闭和深度测试
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);

	// 因为字体要显示在2D空间内，所以必须创建一个正交投影矩阵
	glMatrixMode(GL_PROJECTION); // 指定投影矩阵为当前操作矩阵
	glPushMatrix(); // 保存当前矩阵
	glLoadIdentity(); // 初始化为单位矩阵
	int width = WindowStatus::getSingletonPtr()->w;
	int height = WindowStatus::getSingletonPtr()->h;
	glOrtho(0, width, 0, height, -1, 1);

	glMatrixMode(GL_MODELVIEW); // 指定视图矩阵为当前操作矩阵
	glPushMatrix();  
	glLoadIdentity();  

	//显示字符串
	glColor4f(red, green, blue, alpha);
	glRasterPos3f(x,y, 0); 
	for (size_t i = 0; i < text.size(); ++i) {
		glutBitmapCharacter(mFonts[mCurFont], text[i]); 
	} 

	glPopMatrix(); // 恢复先前的视图矩阵

	// 恢复以前的投影矩阵
	glMatrixMode(GL_PROJECTION); // 指定投影矩阵堆栈为当前操作矩阵 
	glPopMatrix(); // 恢复投影矩阵 
	glMatrixMode(GL_MODELVIEW); // 指定视图堆栈为当前操作堆栈
	glDisable(GL_BLEND); 
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
}

